package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.data.network.entity.ActiveRankEntity;
import com.zw.yzk.kabu.utils.CircleTransform;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/7/5.
 */

public class ActiveRankAdapter extends RecyclerView.Adapter<ActiveRankAdapter.Holder> {

    private List<ActiveRankEntity.Coser> dataList;
    private Context context;

    public ActiveRankAdapter(List<ActiveRankEntity.Coser> list) {
        this.dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public void refresh(List<ActiveRankEntity.Coser> list) {
        this.dataList.clear();
        if (list != null) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void add(List<ActiveRankEntity.Coser> list) {
        int start = dataList.size();
        if (list != null) {
            dataList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }

    public void voteSuccess(int position) {
        if(position <0 || position >= dataList.size()) {
            return;
        }
        dataList.get(position).isVoted = 1;
        notifyItemChanged(position);
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_active_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final ActiveRankEntity.Coser coser = dataList.get(position);

        holder.ranking.setText(String.valueOf(coser.ranking));
        holder.name.setText(coser.nickName);
        holder.ballot.setText(String.valueOf(coser.ballot));

        Glide.with(context)
                .load(coser.headImg)
                .transform(new CircleTransform(context))
                .error(R.drawable.ic_default_head)
                .into(holder.image);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(coser);
                }
            }
        });
        holder.vote.setText(context.getResources().getString(coser.isVoted == 1 ? R.string.voted : R.string.vote));
        holder.vote.setEnabled(coser.isVoted != 1);
        holder.vote.setSelected(coser.isVoted == 1);

        holder.vote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onVote(coser, holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.ranking)
        TextView ranking;
        @Bind(R.id.head)
        ImageView image;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.ballot)
        TextView ballot;
        @Bind(R.id.vote)
        TextView vote;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickedListener {
        void onClick(ActiveRankEntity.Coser coser);

        void onVote(ActiveRankEntity.Coser coser, int position);
    }

    public OnItemClickedListener listener;

    public void setOnItemClickedListener(OnItemClickedListener listener) {
        this.listener = listener;
    }
}
